<?php


namespace App\Services;


# SDK 初始化文件加载
use AdaPaySdk\Drawcash;
use AdaPaySdk\Member;
use AdaPaySdk\CorpMember;
use AdaPaySdk\Payment;
use AdaPaySdk\Refund;
use AdaPaySdk\SettleAccount;
use AdaPaySdk\SettleAccountTransfer;
use Hyperf\Di\Annotation\Inject;
use Laminas\Stdlib\RequestInterface;

include_once dirname(__FILE__) . "/adapay_sdk_php/AdapaySdk/init.php";

# 在文件中设置 DEBUG 为 true 时， 则可以打印日志到指定的日志目录下 LOG_DIR
include_once dirname(__FILE__) . "/adapay_sdk_php/AdapaySdk/config.php";

class AdapayService
{
    public string $appId = 'app_0f97806c-cc89-41db-a4f2-72a7856c1e72';
    public string $prefix = 'adapay_distributor_';
    public string $bus_prefix = 'bus_distributor_';
    public string $custId = '0579750622152704';

    #[inject]
    protected Member $member;
    #[inject]
    protected Payment $payment;
    #[inject]
    protected Refund $refund;
    #[inject]
    protected SettleAccount $settleAccount;
    #[inject]
    protected Drawcash $drawcash;
    #[inject]
    protected SettleAccountTransfer $settleAccountTransfer;
    #[inject]
    protected CorpMember $corpMember;



    public function __construct(){

    }

    /**通用返回
     * @param array $params
     * @return array
     */
    public function utilReturn(array $params):array{
        if(isset($params[0])){
            $data['code'] = $params[0];
        }else{
            $data['code'] = 403;
        }
        if(isset($params[1])) {
            $data['data'] = \Qiniu\json_decode($params[1], true);
            $data['data'] = \Qiniu\json_decode($data['data']['data'], true);
        }else{
            $data['data'] = $params;
        }
        return $data;
    }

    /**创建用户
     * @param array $params
     * @return array
     */
    public function createMember(array $params):array{
# 创建
        $this->member->create($params);
        return $this->utilReturn($this->member->result);
    }

    /**
     * 创建企业
     */
    public function corpMember(array $params):array{
        $this->corpMember->create($params);
        return $this->utilReturn($this->corpMember->result);
    }

    public function updateCorp(array $params):array{
        $this->corpMember->update($params);
        return $this->utilReturn($this->corpMember->result);
    }

    public function queryCorp(array $params):array{
        $this->corpMember->query($params);
        return $this->utilReturn($this->corpMember->result);
    }

    /**查询用户对象
     * @param $memberId
     * @return array
     */
    public function queryMember(int $memberId):array{
        $this->member->query(["app_id"=> $this->appId, "member_id"=>$this->prefix.$memberId]);
        return $this->utilReturn($this->member->result);
    }

    /**
     * @return void
     */
    public function Payment($payment_params)
    {
        # 支付设置
        $this->payment->create($payment_params);
        return $this->utilReturn($this->payment->result);
    }

    /**关闭订单
     * @return void
     */
    public function closePay()
    {
        # 初始化支付类
        $payment_params = array(
            # 设置支付对象ID
            "payment_id" => "002212024011415554110592515340170354688",
            # 设置描述
            "reason" => "关单测试",
            # 设置扩展域
            "expend"=> [],
        );
        # 发起关单
        $this->payment->close($payment_params);
        # 对关单结果进行处理
        # $payment->result 类型为数组
        if ($this->payment->isError()) {
            //失败处理
            var_dump($this->payment->result);
        } else {
            //成功处理
            var_dump($this->payment->result);
        }
    }

    /**退款
     * @return void
     */
    public function refundPayment($refund_params){
        $this->refund->create($refund_params);
        return $this->utilReturn($this->refund->result);
    }

    /**退款查询
     * @return void
     */
    public function queryRefund(){
# refund_id或charge_id二选一
# 发起退款查询
        $this->refund->query(["payment_id"=> "002112019091919132010020717028587851776"]);
# 对退款结果进行处理
# $refund->result 类型为数组
        if ($this->refund->isError()){
            //失败处理
            var_dump($this->refund->result);
        } else {
            //成功处理
            var_dump($this->refund->result);
        }
    }

    /**创建结算账户
     * @param array $account_params
     * @return array
     */
    public function settleAccount(array $account_params):array{
# 创建结算账户
        $this->settleAccount->create($account_params);
# 对创建结算账户结果进行处理
        return $this->utilReturn($this->settleAccount->result);
    }

    /**删除结算账户对象
     * @param array $params
     * @return array
     */
    public function delAccount(array $params):array{
        $this->settleAccount->delete($params);
        return $this->utilReturn($this->settleAccount->result);
    }

    /**查询用户余额
     * @param array $params
     * @return array
     */
    public function accountBalance(array $params):array{
# 查询账户余额
        $this->settleAccount->balance($params);
# 对查询账户余额结果进行处理
        return $this->utilReturn($this->settleAccount->result);
    }

    /**余额支付
     * @param array $params
     * @return array
     */
    public function accountTransfer(array $params):array{
        $this->settleAccountTransfer->balancePay($params);
        return $this->utilReturn($this->settleAccountTransfer->result);
    }

    /**余额退款
     * @param array $params
     * @return array
     */
    public function accountBalanceRefund(array $params):array{
        $this->settleAccountTransfer->balanceRefund($params);
        return $this->utilReturn($this->settleAccountTransfer->result);
    }


    /**用户取现
     * @param array $params
     * @return array
     */
    public function drawcash(array $params):array{
        $this->drawcash->create($params);
        return $this->utilReturn($this->drawcash->result);
    }

}
